// pages/orderdetail/orderdetail.js
import Dialog from '../../vant/dialog/dialog';
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    show:false,
    // isshare:false,
    shop:{
      shopId: 1,
      shopName: 'Marvel',
      shopImg: 'http://106.12.192.199/marvel.png',
    },
    product: {
      title:"钢铁侠促销",
      desc:'钢铁侠便宜卖了钢铁侠便宜卖了钢铁侠便宜卖了钢铁侠便宜卖了钢铁侠便宜卖了钢铁侠便宜卖了',
      isSale:false,
      saleDeadLine:'2019/8/27 17:00:00',
      originPrice:'1000',
      salePrice:'100',
      imgUrlList: [{ type: 'image', url:'../../images/product-1.jpg'},
        { type: 'image', url: '../../images/product-2.jpg' },
        { type: 'image', url: '../../images/product-3.jpg' },
        { type: 'image', url: '../../images/product-4.jpg' }],
      postPrice: 10,
      saledNum: 1000,
      detailImg:'/images/product_detail_1.jpg',
      
    },
    countDown:'',
    buyShow: false,
    buyNums:1,
    buyStatus:0,//0 加入购物车 1 下单
    cart: null
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this
    // if (options.productId) {
    //   that.setData({
    //     isshare:true
    //   })
    // }
   
    var date = new Date(that.data.product.saleDeadLine);
    var deadTime = date.getTime();
    var nowTime = new Date().getTime();
    var lastTime = deadTime - nowTime;
    var timeout = setInterval(function () {
      lastTime = lastTime - 1000;
      if (lastTime < 0) {
        clearInterval(timeout);
        that.setData({
          ["product.isSale"]:false
        })
      }else{
        that.setData({
          ["product.isSale"]: true
        })
      }
      var day = Math.floor(lastTime / 1000 / 60 / 60 / 24);
      var hour = Math.floor(lastTime % (1000 * 60 * 60 * 24) / 1000 / 60 / 60);
      var minute = Math.floor(lastTime % (1000 * 60 * 60) / 1000 / 60);
      var seconds = Math.floor(lastTime % (1000 * 60) / 1000);
      day = day == 0 ? '' : day + "天"
      hour = hour == 0 ? '' : hour + "时"
      minute = minute == 0 ? '' : minute + "分"
      seconds = seconds == 0 ? '' : seconds + "秒"
      that.setData({
        countDown: day + hour + minute + seconds,
        show:true
      });
    }, 1000)

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    this.setData({
      cart: app.globalData.cart
    })
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
   
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    return {
      title: this.data.product.title,
      path: '/pages/orderdetail/orderdetail?productId=111'
    }
  },
  home(){
    wx.switchTab({
      url: '/pages/index/index',
    })
  },
  onClose() {
    this.setData({ buyShow: false });
  },
  addCart(){
    this.setData({
      buyStatus: 0,
      buyShow: true
    })
  },
  buy(){
    this.setData({
      buyStatus: 1,
      buyShow: true
    })
  },
  remove(){
    if (this.data.buyNums > 1){
      this.setData({
        buyNums: --this.data.buyNums
      })
    }
  },
  add(){
    this.setData({
      buyNums: ++this.data.buyNums
    })
  },
  sureAddCart(){
    Dialog.confirm({
      title: '提示',
      message: '确定加入购物车？'
    }).then(() => {
      let item ={
        shopId:this.data.shopId,
        product: this.data.product,
        nums: this.data.buyNums
      }
      app.globalData.cart.nums++
      app.globalData.cart.items.push(item)
      this.setData({
        ['cart.nums']: app.globalData.cart.nums,
        buyShow: false
      })
    }).catch(() => {
      // on cancel
    });
    
  },
  sureBuy(){
    this.setData({
      buyShow: false
    })
  },
  toCart(){
    wx.reLaunch({
      url: '/pages/cart/cart',
    })
  }
})